<?php

namespace App\Http\Controllers\Admin\Risk;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class AlipayRiskController extends BaseController {

    /**
     * @api {post} /risk/alipay 支付宝风控创建
     * @apiVersion 0.1.0
     * @apiName 风控-支付宝风控创建
     * @apiGroup 风控
     *
     *
     * @apiParam {number} level                         开启等级
     * @apiParam {number} rank_id                       用户等级
     * @apiParam {number} register_time                 注册时间
     * @apiParam {number} recharge_max_day              每日最高充值
     * @apiParam {number} recharge_max_single           单笔最高充值
     * @apiParam {number}  is_force                     强制限制 1是0否
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'level' => 'required|numeric|integer|min:1',
                'rank_id' => 'required|numeric|integer|min:1',
                'register_time' => 'required|numeric|integer|min:1',
                'recharge_max_day' => 'required|numeric|integer|min:1',
                'recharge_max_single' => 'required|numeric|integer|min:1',
                'is_force' => 'required|numeric|in:0,1',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        $res = app('repo_risk');
        $params['add_time'] = time();
        $params['add_date_time'] = date("Y-m-d H:i:s");

        $rows = $res->addRechargeLimit($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /risk/alipay 支付宝风控删除
     * @apiVersion 0.1.0
     * @apiName 风控-支付宝风控删除
     * @apiGroup 风控
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_risk')->delRechargeLimit($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /risk/alipay 支付宝风控修改
     * @apiVersion 0.1.0
     * @apiName 风控-支付宝风控修改
     * @apiGroup 风控
     *
     *
     * @apiParam {number} id
     * @apiParam {number} level                         开启等级
     * @apiParam {number} rank_id                       用户等级
     * @apiParam {number} register_time                 注册时间
     * @apiParam {number} recharge_max_day              每日最高充值
     * @apiParam {number} recharge_max_single           单笔最高充值
     * @apiParam {number}  is_force                     强制限制 1是0否
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'level' => 'required|numeric|integer|min:1',
                'rank_id' => 'required|numeric|integer|min:1',
                'register_time' => 'required|numeric|integer|min:1',
                'recharge_max_day' => 'required|numeric|integer|min:1',
                'recharge_max_single' => 'required|numeric|integer|min:1',
                'is_force' => 'required|numeric|in:0,1',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $id = $request->input('id');
        $res = app('repo_risk');

        $params = $request->input();

        $rows = $res->saveRechargeLimit(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /risk/alipay 支付宝风控列表
     * @apiVersion 0.1.0
     * @apiName 风控-支付宝风控列表
     * @apiGroup 风控
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "id": 19,
                "level(开启等级)": "6",
                "rank_id(用户等级)": 23,
                "register_time(注册时间)": 300,
                "is_force(强制限制 1是0否)": 1,
                "recharge_max_day(每日最高充值)": 2222,
                "recharge_max_single(单笔最高充值)": 2222,
                "add_date(添加时间)": "2020-09-22"
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_risk')->getRechargeLimitQueryList($request->input());

        return Response::sendList($list);
    }


}
